package assetservicelogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/BeanUtils"
	"property_service/utils/result"
)

type AssetCheckSheetAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAssetCheckSheetAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AssetCheckSheetAddLogic {
	return &AssetCheckSheetAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// AssetCheckSheetAdd 创建资产盘点单表项
func (l *AssetCheckSheetAddLogic) AssetCheckSheetAdd(in *property_service.AssetCheckSheet) (*property_service.ResponseId, error) {
	// 获取资产盘点单表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).AssetCheckSheet
	// 创建一个新的资产盘点单表模型对象，不包含ID、TenantID、CreatedAt、UpdatedAt字段
	mod := &model.AssetCheckSheet{
		CheckCode:     in.CheckCode,                          // 盘点单号
		CheckDate:     BeanUtils.GetDateFormat(in.CheckDate), // 盘点日期
		CheckType:     in.CheckType,                          // 盘点类型
		CheckDept:     in.CheckDept,                          // 盘点科室
		CheckDeptID:   in.CheckDeptID,                        // 盘点科室ID
		CheckPerson:   in.CheckPerson,                        // 盘点人
		CheckPersonID: in.CheckPersonID,                      // 盘点人ID
		Remarks:       in.Remarks,                            // 备注
		Count:         in.Count,                              // 资产数
		StatusName:    in.StatusName,                         // 状态名称
		StatusID:      in.StatusID,                           // 状态ID
		StockID:       in.StockID,                            // 仓库ID
		TenantID:      in.TenantID,                           // 租户ID
		CreatedAtuser: in.CreatedAtuser,                      // 创建人
		UpdatedAtuser: in.UpdatedAtuser,                      // 更新人
	}
	// 使用上下文执行创建操作
	if err := m.WithContext(l.ctx).Create(mod); err != nil {
		res, err := result.Err(err).Ed()
		return l.buildResponse(mod.ID, res, err)
	}
	// 创建成功，返回成功响应
	res, err := result.Suc().AddEd()
	return l.buildResponse(mod.ID, res, err)
}

// 在 AssetCheckSheetAddLogic 结构体中添加辅助方法
func (l *AssetCheckSheetAddLogic) buildResponse(id int64, res *property_service.Response, err error) (*property_service.ResponseId, error) {
	return &property_service.ResponseId{
		Id:      id,
		Code:    res.Code,
		Message: res.Message,
	}, err
}
